package com.glsc.ngateway.oaflow.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;


/**
 * 版权所有：国联证券信息技术管理部
 * 系统名称：股衍中证报送平台
 * 文件名称：DateUtils.java
 * 创建时间：2020-11-20
 * 文件描述：日期Utils类
 * 文件作者：张宁
 */

public class DateUtils {


    public static String formatSecond = "yyyy-MM-dd HH:mm:ss";

    public static String formatDate = "yyyyMMdd";

    public static String formatDate_ = "yyyy-MM-dd";

    public static String formatSecondSimple = "yyyyMMddHHmmss";


    public static String formatMillisecond = "yyyyMMddHHmmssSSS";


    public static String formatHour = "HH";


    /**
     * 功能描述:
     *
     * @param: 获取当前系统时间 yyyy-MM-dd HH:mm:ss
     * @return:
     * @auther: youqing
     * @date: 2019/10/8 9:58
     */
    public static String getCurrentDate(String formatSecondSimple) {
        SimpleDateFormat df = new SimpleDateFormat(formatSecondSimple);
        String date = df.format(System.currentTimeMillis());
        return date;
    }


    public static String getCurrentDate() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = df.format(System.currentTimeMillis());
        return date;
    }


    public static String getDelayOneDate() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        Date d = minusDay(new Date(), 1);
        String date = df.format(d);
        return date;
    }


    /**
     * 功能描述:
     *
     * @param: date类 获取当前系统时间 yyyy-MM-dd HH:mm:ss
     * @return:
     * @auther: youqing
     * @date: 2019/10/8 9:58
     */
    public static Date getCurrentDateToDate() {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = df.format(System.currentTimeMillis());
        Date d = null;
        try {
            d = df.parse(date.toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }

    /**
     * 增加时间单位：天
     *
     * @param day
     * @return
     */
    public static String getCurrentAddDay(int day) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DATE, day);
        return sdf.format(cal.getTime());
    }

    /**
     * 增加时间单位：分钟
     *
     * @param minute
     * @return
     */
    public static String getCurrentAddMin(int minute) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.MINUTE, minute);
        return sdf.format(cal.getTime());
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getNowDateString() {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(d);
    }


    /**
     * 把Date转为String
     *
     * @param date
     * @param format
     * @return
     */
    public static String getFormatTime(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     * 增加时间单位：天
     *
     * @param day
     * @return
     */
    public static Date addDay(int day) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DATE, day);
        return cal.getTime();
    }

    /**
     * 增加时间单位：天
     *
     * @param date
     * @param day
     * @return
     */
    public static Date addDay(Date date, int day) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, day);
        return cal.getTime();
    }

    /**
     * @param date
     * @param day
     * @return
     * @Descrip 减去多少天
     */
    public static Date minusDay(Date date, int day) {
        return addDay(date, -day);
    }

    public static String cstDateTrans(String dateStr) {

        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);

        //java.util.Date对象
        Date date;
        try {
            date = (Date) sdf.parse(dateStr);
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }

    }

    public static String cstDateTransDate(String dateStr) {

        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);

        //java.util.Date对象
        Date date;
        try {
            date = (Date) sdf.parse(dateStr);
            return new SimpleDateFormat("yyyyMMdd").format(date);

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }

    }

    public static String cstDateTransSimpleDate(String dateStr) {

        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);

        //java.util.Date对象
        Date date;
        try {
            date = (Date) sdf.parse(dateStr);
            return new SimpleDateFormat("yyyy-MM-dd").format(date);

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }

    }


    /**
     * @param dateStr
     * @return
     * @Description 判断字符串能否转化为 日期类型
     */
    public static Boolean isDateString(String dateStr) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        boolean convertSuccess = true;
        // 指定日期格式为四位年/两位月份/两位日期，注意yyyy/MM/dd区分大小写；
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        try {
            //设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
            format.setLenient(false);
            format.parse(dateStr);
        } catch (ParseException e) {
            convertSuccess = false;
        }
        return convertSuccess;
    }

    public static String today() {
        LocalDate today = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        return today.format(formatter);
    }

    public static void main(String[] args) {


//        System.out.println(getCurrentDate(formatSecondSimple));

        System.out.println(getCurrentDate(formatDate));


        List<String> dataList = new ArrayList<String>();

        if (dataList.isEmpty()) {
            System.out.println("WWWWWWWWW");
        }


        //formatDate

//    	if(DateUtils.isDateString(test)) {
//    		System.out.println("能够进行类型转化wwwww");
//    	}else {
//    		System.out.println("不能够进行类型转化");
//    	}
//

//    	System.out.println(DateUtils.getDelayOneDate());


    }
}
